package com.xlh.adapter;

import java.util.List;

import com.xlh.Utils.Constant;
import com.xlh.Utils.FUti;
import com.xlh.bean.TitleBean;
import com.xlh.outside.R;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.graphics.drawable.GradientDrawable;

public class BtnsFgAdapter extends BaseAdapter {

	private List<TitleBean> data;
	private LayoutInflater mInflater;

	public BtnsFgAdapter(Context context, List<TitleBean> data) {
			// TODO Auto-generated constructor stub
			mInflater = LayoutInflater.from(context);
			this.data = data;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data == null ? 0 : data.size();
	}
	public TitleBean getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder ;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.btn_item, parent, false);
			holder = new ViewHolder();
			holder.tv01 = (TextView) convertView.findViewById(R.id.mBtnText01);
			holder.tv02 = (TextView) convertView.findViewById(R.id.mBtnText02);
			
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}

		TitleBean titleBean = data.get(position);
		//String butText = titleBean.getmText().substring(0, 2);
		String but1Text = "";
		String but2Text = "";


		if (titleBean.getmText().length()<2)
		{
			but1Text = titleBean.getmText().substring(0, titleBean.getmText().length());
			but2Text = "";
		}else{
			but1Text = titleBean.getmText().substring(0, 2);
			but2Text = titleBean.getmText().substring(2);
		}

		//convertView.setBackgroundColor(Color.parseColor("#ffff66"));
		//#e67300

//		GradientDrawable drawable = (GradientDrawable) convertView.getBackground();
//		//边框
//		drawable.setStroke(FUti.dip2px(convertView.getContext(),1), Color.parseColor(Constant.getAppColor().getButBorderColor()));
//		//设置背景
//		drawable.setColor(Color.parseColor(Constant.getAppColor().getButBackColor()));

		holder.tv01.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));
		holder.tv02.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));


		if ( titleBean.getColor()==null ){
			holder.tv01.setText(but1Text);
			holder.tv02.setText(but2Text);


		}else{
			//这是按钮的上一排文字
			holder.tv01.setText(Html.fromHtml("<font color='" + titleBean.getColor() + "'>" + but1Text + "</font>"));
			//holder.tv01.setText(titleBean.getmText().substring(0, 2));
			//这是按钮的下一排文字..
			//截取必须争取 否则报错
			holder.tv02.setText(Html.fromHtml("<font color='" + titleBean.getColor() + "'>" + but2Text + "</font>"));
			//holder.tv02.setText(Html.fromHtml("<font color='" + titleBean.getColor() + "'>" + titleBean.getmText().substring(2) + "</font>"));
			//holder.tv02.setText(titleBean.getmText().substring(2));
		}

		return convertView;
	}

	private class ViewHolder {
		
		private TextView tv01;
		private TextView tv02;
		
	}
	
	public void addTitle(TitleBean bean) {
		// TODO Auto-generated method stub
		if (data != null) {
			data.add(bean);
		}
		this.notifyDataSetChanged();
	}

	public void setData(List<TitleBean> mTitleData) {
		// TODO Auto-generated method stub
		if(mTitleData != null) {
			this.data = mTitleData;
		}
		this.notifyDataSetChanged();
	}

	public void clear() {
		// TODO Auto-generated method stub
		if(data != null) {
			data.clear();
		}
		this.notifyDataSetChanged();
	}

	public void addTitle(List<TitleBean> ts) {
		// TODO Auto-generated method stub
		
		if(this.data != null) {
			
			for (int i = 0; i < ts.size(); i++) {
				this.data.add(ts.get(i));
			}
		}
		this.notifyDataSetChanged();
	}
	
}
